#include "utilisateur.h"
#include <string.h>

int nSpaces = 0;

int main ( int argc , char **argv)
{
	FILE* inserver;
	char name[100];
	char newFile[100];
	char** list;
	char type;
	int listsize;
	int i;

	parseArgAndDupFiles(argc,argv, DUP_OUT, NULL, NULL);
	inserver = fdopen(atoi(argv[1]),"r");

	while(1)
	{
		fprintf(stderr,"# ");
		if(fgets(name, 100, stdin) == NULL)
		{
			fprintf (stderr, "Error in fgets.\n");
			continue;
		}
		name[strlen (name) -1] = '\0';	
		nSpaces = 0;
		switch(verifyName(name))
		{
			case(COMMAND_CORRECT):
				if(strstr(name,"ADD") == name){
					sprintf(newFile, "%s", name+4);
					if(!sendOperation(inserver,stdout,OP_ADD,newFile))
						fprintf(stderr, "File already exists or full disk: %s\n", newFile );
					else
						fprintf(stderr, "Success, file %s added to server\n", newFile );

				}else if (strstr(name,"LST") == name){
					if(!askListToServer(inserver,&list,&listsize)){
						fprintf(stderr,"Server doesn't resoponded\n");
						fflush(stderr);
						break;
					}
					for(i = 0; i<listsize;i++){
						fprintf(stderr,"%s ",list[i]);
					}
					fprintf(stderr,"\n");
				}else if (strstr(name,"DEL") == name){
					sprintf(newFile, "%s", name+4);
					if(!sendOperation(inserver,stdout,OP_DEL,newFile))
						fprintf(stderr, "Could not remove: %s\n", newFile );
					else
						fprintf(stderr, "Success, file %s removed from server\n", newFile );
				}else if (strstr(name,"ALL") == name){
					if(!askClientListToServer(inserver,stdout,&list,&listsize)){
						fprintf(stderr,"Server didn't responded\n");
						fflush(stderr);
						sleep(50);
						break;
					}
					for(i = 0; i<listsize;i++){
						fprintf(stderr,"%s ",list[i]);
					}
					fprintf(stderr,"\n");
				}else if (strstr(name,"REG") == name){
					sscanf(name+4, " %c %s",&type,2+newFile);
					if((!(type == 'P' || type == 'G' || type == 'U' || type == 'C' || type == 'E' )) || strlen(newFile) < 1){
						fprintf(stderr, "Syntax error: REG [P|G|U|C|E] <name>\n");
						break;
					}
					newFile[0] = type;
					newFile[1] = ' ';
					if(!sendOperation(inserver,stdout,OP_REG,newFile))
						fprintf(stderr, "Could not register client: %s\n", newFile);
					else
						fprintf(stderr, "Success, client registered %s\n", newFile );
				}else if (strstr(name,"UNR") == name){
					sprintf(newFile, "%s", name+4);
					if(!sendOperation(inserver,stdout,OP_UNR,newFile))
						fprintf(stderr, "Could not remove: %s\n", newFile );
					else
						fprintf(stderr, "Success, client %s removed from server\n", newFile );
				}	
				break;
			case(CHAR_INCORRECT):
				if(nSpaces > 0)
					fprintf (stderr,"Only one space is permitted in the command.\n");
				else
					fprintf (stderr,"Error in the name of the video. Forbidden character(s) used.\n");
				break;
			default:
				fprintf (stderr,"Command not found. Possibles commands: ADD <name>.<ext> or LST\n");
					
		}
	}
	exit(EXIT_SUCCESS);
}


int verifyName(char *name)
{
	int i;
	char t;
	
	t = name[0];
	if (t == '\0' || t == '\n')
		return 0;

	for(i = 1; t != '\0' && t != '\n'; i++)
	{
		if (!isValidChar(t))
			return CHAR_INCORRECT;
		t = name[i];
	}
	
	return COMMAND_CORRECT;
}

int isValidChar(const char c)
{
	if (nSpaces > 2)
	{
		return 0;
	}
	if (c == ' ')
	{
		nSpaces++;
		return 1;
	}
	if ((c >= '0' && c <= '9') || (c == '.'))
		return 1;
	if (c >= 'A' && c <= 'Z')
		return 1;
	if (c >= 'a' && c <= 'z')
		return 1;

	return 0;
}
